package com.wolf.wztoyy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

import org.json.JSONObject;

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;

public enum Test6 {

	INSTANCE;
	
	public static Test6 getInstance(){
		return INSTANCE;
	}
	
	public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";
    public static AipSpeech client = null;
    
    static {


        client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }
    
    public void synthesis(String text)
    {

        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("spd", "5");
        options.put("pit", "5");
        options.put("per", "4");
        TtsResponse res1 = client.synthesis(text, "zh", 1, options);
        System.out.println(res1.getErrorCode());
        byte[] data = res1.getData();
        save(data);
    }
	
    public void save(byte[] b){
    	String filePath = "F:/Testly/语音文件/789.mp3";
    	File file = new File(filePath);  
    	if(file.exists()){
    		file.delete();
    	}
    	new Play(operaFileData(filePath,b)).play();
    }
    
    public AudioFormat getAudioFormat()   
    {  
        AudioFormat.Encoding encoding = AudioFormat.Encoding.  
        PCM_SIGNED ;  
        float rate = 8000f;  
        int sampleSize = 16;  
        String signedString = "signed";  
        boolean bigEndian = true;  
        int channels = 1;  
        return new AudioFormat(encoding, rate, sampleSize, channels,  
                (sampleSize / 8) * channels, rate, bigEndian);   
    }  
    
    public File operaFileData(String path, byte[] by)
    {
    {
    FileOutputStream fileout = null;
    String fileName = path;
    File file = new File(fileName);
    if (file.exists())
    {
    file.delete();
    }
    try
    {
    fileout = new FileOutputStream(file);
    fileout.write(by, 0, by.length);

    } catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally
    {
    try
    {
    fileout.close();
    } catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    return file;
    }

    }
  	
}
